import tkinter as tk
from tkinter import messagebox
from tkinter import colorchooser


class application(tk.Frame):
    def __init__(self, master=None, width=1000, height=500):
        self.master = master
        self.width = width
        self.height = height
        self.bg = 'pink'
        self.color = '#0000ff'
        super().__init__(master)
        self.x = 0
        self.y = 0
        self.lastDraw = None
        self.creatWidget()

    def creatWidget(self):
        self.drawpad = tk.Canvas(self.master, bg=self.bg, width=self.width, height=self.height * 0.85)
        self.drawpad.grid(row=0, column=0, columnspan=8, sticky='ew')
        self.btnStart = tk.Button(self.master, text='开始', name='start')
        self.btnStart.grid(row=1, column=0, pady=30)
        self.btnPen = tk.Button(self.master, text='画笔', name='pen')
        self.btnPen.grid(row=1, column=1, pady=30)
        self.btnRect = tk.Button(self.master, text='矩形', name='rect')
        self.btnRect.grid(row=1, column=2, pady=30)
        self.btnClear = tk.Button(self.master, text='清屏', name='clear')
        self.btnClear.grid(row=1, column=3, pady=30)
        self.btnEraser = tk.Button(self.master, text='橡皮擦', name='earser')
        self.btnEraser.grid(row=1, column=4, pady=30)
        self.btnLine = tk.Button(self.master, text='直线', name='line')
        self.btnLine.grid(row=1, column=5, pady=30)
        self.btnLinearrow = tk.Button(self.master, text='箭头直线', name='linearrow')
        self.btnLinearrow.grid(row=1, column=6, pady=30)
        self.btnColor = tk.Button(self.master, text='颜色', name='color')
        self.btnColor.grid(row=1, column=7, pady=30)
        # 事件管理
        self.btnStart.bind_class("Button", "<Button-1>", self.eventManager)
        # 快捷键
        self.master.bind("<KeyPress-y>",self.colorYellow)

    def colorYellow(self,event):
        self.color='yellow'

    def eventManager(self, event):
        name = event.widget.winfo_name()

        if name == 'line':
            self.drawpad.bind("<Button-1>", self.startDraw)
            self.drawpad.bind('<B1-Motion>', self.drawLine)
            self.drawpad.bind('<ButtonRelease-1>', self.stopDraw)
        elif name == 'linearrow':
            self.drawpad.bind("<Button-1>", self.startDraw)
            self.drawpad.bind('<B1-Motion>', self.drawLinearrow)
            self.drawpad.bind('<ButtonRelease-1>', self.stopDraw)
        elif name == "rect":
            self.drawpad.bind("<Button-1>", self.startDraw)
            self.drawpad.bind("<B1-Motion>", self.drawRect)
            self.drawpad.bind("<ButtonRelease-1>", self.stopDraw)
        elif name == 'color':
            self.color = colorchooser.askcolor()[-1]
        elif name == 'pen':
            self.drawpad.bind("<B1-Motion>", self.drawPen)
        elif name == 'earser':
            self.drawpad.bind("<Button-1>", self.startDraw)
            self.drawpad.bind("<B1-Motion>", self.drawEarser)
            self.drawpad.bind("<ButtonRelease-1>", self.stopDraw)
        elif name == 'clear':
            self.drawpad.delete(tk.ALL)

    def startDraw(self, event):
        self.x = event.x
        self.y = event.y

    def drawLine(self, event):
        self.drawpad.delete(self.lastDraw)
        self.lastDraw = self.drawpad.create_line(self.x, self.y, event.x, event.y, fill=self.color)

    # print(f'self.x={self.x},self.y={self.y},x={event.x}，y={event.y}')

    def drawLinearrow(self, event):
        self.drawpad.delete(self.lastDraw)
        self.lastDraw = self.drawpad.create_line(self.x, self.y, event.x, event.y, arrow=tk.LAST, fill=self.color)

    def drawRect(self, event):
        self.drawpad.delete(self.lastDraw)
        self.lastDraw = self.drawpad.create_rectangle(self.x, self.y, event.x, event.y, outline=self.color,
                                                      fill=self.color)

    def stopDraw(self, event):
        self.lastDraw = None

    def drawPen(self, event):
        self.drawpad.create_oval(event.x, event.y, event.x + 2, event.y + 2, outline=self.color, fill=self.color)

    def drawEarser(self, event):
        self.drawpad.delete(self.lastDraw)
        self.lastDraw = self.drawpad.create_rectangle(self.x, self.y, event.x, event.y, outline=self.bg, fill=self.bg)

    def test(self, event):
        messagebox.showinfo('title', 'test')


width = 1000
height = 500
window = tk.Tk()
window.title('Drawpad')
window.geometry(f"{width}x{height}")
app = application(window, width, height)
window.mainloop()
